#include "view_others_stopwatch.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_others_device_info.h"
#include "view_common.h"
#include "app_trace.h"
#include "stopwatch.h"
#include "language_text.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_others_stopwatch_display(void)
{
#ifdef CUSTOM_UI
    int const temp_index_start = 50;
    int custom_ui_index = 0;
    for (int i = 0; i < STOP_WATCH_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_stop_watch_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if STOP_WATCH_GIF_COUNT > 0
    disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_stop_watch_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif
    int x = 60;
    int y = 182;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 1;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, x, y, FILE_IMG_9202_shopwatch_text);
    }
    //    bool ret = disp_show_localize(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_string_key_stop_watch_text, temp_index_start + custom_ui_index);
    // if (!ret)
    // {
    //     _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, 76, 177, FILE_IMG_3002_main_text);
    // }
    ++custom_ui_index;

#else
    int x = 58;
    int y = 36;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_9201_shopwatch_icon);
    x = 60;
    y = 182;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 1;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_9202_shopwatch_text);
    }
    show_button_up();
    show_button_right();
    show_button_down();
#endif
}

typedef enum __stop_watch_type_t
{
    stop_watch_type_main,
    stop_watch_type_other,
} stop_watch_type_t;
static void _disp_show_stopwatch(uint32_t time, int start_y, stop_watch_type_t type)
{
    int font_width = 0;
    int font_interval = 0;
    int point_interval = 0;
    int point_width = 0;
    int ms_interval = 0;
    int x = 0;
    uint8_t const *const *font_tab = 0;
    uint8_t const *point = 0;
    uint32_t temp32 = time;
    uint8_t temp8 = (temp32 % 1000) / 10;
    int point_y_offset = 0;
    switch (type)
    {
    case stop_watch_type_main:
        font_width = 14;
        font_interval = 3;
        point_interval = 6;
        point_width = 6;
        ms_interval = 4;
        font_tab = num_N300_dig_14x32_TAB;
        point = num_N312_second_6x32;
        x = 52;
        break;
    case stop_watch_type_other:
        font_width = 12;
        font_interval = 2;
        point_interval = 3;
        point_width = 4;
        ms_interval = 4;
        font_tab = num_N200_dig_12x24_TAB;
        point = num_N213_pace_4x14_dot;
        point_y_offset = 5;
        x = 86;
        break;
    default:
        return;
    }

    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval, start_y + 1, num_N101_dig_7x15_TAB[temp8 / 10]); //毫秒
    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval + 9, start_y + 1, num_N101_dig_7x15_TAB[temp8 % 10]);

    temp32 /= 1000;
    disp_draw_user_image(x + 4 * font_width + 2 * point_width + 2 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //秒
    disp_draw_user_image(x + 5 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 4 * font_width + 1 * point_width + 2 * font_interval + 3 * point_interval, start_y + point_y_offset, point);

    temp32 /= 60;
    disp_draw_user_image(x + 2 * font_width + 1 * point_width + 1 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //分
    disp_draw_user_image(x + 3 * font_width + 1 * point_width + 2 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 2 * font_width + 0 * point_width + 1 * font_interval + 1 * point_interval, start_y + point_y_offset, point);

    temp32 /= 60;
    disp_draw_user_image(x + 0 * font_width + 0 * point_width + 0 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //时
    disp_draw_user_image(x + 1 * font_width + 0 * point_width + 1 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
}

void view_others_stopwatch_state_display(void)
{
    int x = 0;
    int y = 0;
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = stopwatch_get_catch_time(0);
        disp_ctrl.com_data.i_temp[1] = stopwatch_get_catch_time(1);
        disp_ctrl.com_data.i_temp[2] = stopwatch_get_catch_time(2);
        disp_ctrl.com_data.i_temp[3] = stopwatch_get_catch_time(3);
        disp_ctrl.com_data.i_temp[4] = stopwatch_state();
        display_set_update_area(52, 179, 150, 32); //!< 73, 75,225, 192 200 195
        disp_ctrl.auto_close_delay = 10000;
        if (disp_ctrl.com_data.i_temp[4] == stop_watch_state_start)
        {
            disp_ctrl.auto_refresh_time = 22;
        }
        else
        {
            disp_ctrl.auto_refresh_time = 0;
        }
    }
    x = 46;
    y = 30;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_9203_shopwatch_1);
    y = 82;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_9204_shopwatch_2);
    y = 132;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_9205_shopwatch_3);

    x = 224;
    y = 111;
    if (disp_ctrl.com_data.i_temp[4] == stop_watch_state_start)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_9207_shopwatch_end);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_9206_shopwatch_start);
    }
    show_button_up();
    show_button_down();
    //主时间
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[0], 179, stop_watch_type_main);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[1], 33, stop_watch_type_other);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[2], 85, stop_watch_type_other);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[3], 138, stop_watch_type_other);
}

static void refresh(void)
{
    disp_ctrl.area_update_flag = 0;
}
static void move_back_view(void)
{
    display_show(view_others_stopwatch, DISP_MODE_NORMAL);
    stopwatch_stop();
    refresh();
}
static void start_and_pause(void)
{
    stop_watch_state_t state = stopwatch_state();
    if (state == stop_watch_state_start)
    {
        stopwatch_stop();
    }
    else
    {
        stopwatch_resume();
    }
    refresh();
}

view_event_result_t view_others_stopwatch_state_event(event_param_t param)
{
    switch (param.source)
    {
    case event_source_system:
        switch (param.event)
        {
        case event_view_entry:
        {
            stopwatch_reset();
            // uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            // uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            // disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Stopwatch, buff, buff_len);
            // return view_event_result_handled;
        }
        case event_view_exit:
            stopwatch_stop();
            break;
        }
        break;
#if defined(BUTTON_TOP)
    case event_source_button_top:
        if (param.event == BUTTON_EVENT_SHORT_PRESS)
        {
            stopwatch_catch_time();
            refresh();
        }
        break;
#endif
#if defined(BUTTON_MIDDLE)
    case event_source_button_middle:
        switch (param.event)
        {
        case BUTTON_EVENT_SHORT_PRESS:
            start_and_pause();
            break;
        case BUTTON_EVENT_LONG_PRESS:
            move_back_view();
            break;
        }
        break;
#endif
#if defined(BUTTON_BOTTOM)
    case event_source_button_bottom:
        if (param.event == BUTTON_EVENT_SHORT_PRESS)
        {
            stopwatch_reset();
            refresh();
        }
        break;
#endif
    case BUTTON_TOUCH:
    {
        switch (param.event)
        {
        case BUTTON_EVENT_TP_MOVE_RIGHT:
            move_back_view();
            break;
        case BUTTON_EVENT_TP_TAP:
            start_and_pause();
            break;
        }
    }
    break;
    }
    return view_event_result_handled;
}
